package com.yanxu.design01_singleton.destroy.serialize_destory;

import java.io.*;

/**
 * @author 折戟沉沙铁未销
 * @version V1.0
 * @date 2025/4/14-2025
 * @Description: 使用序列化的方式破坏坏单例模式
 */
public class HungryTest {
    public static void main(String[] args) throws IOException, ClassNotFoundException {
        HungryInstance instance = HungryInstance.getInstance();
        System.out.println("instance = " + instance);

        ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("singleton_file"));
        oos.writeObject(instance);

        File file = new File("singleton_file");
        ObjectInputStream ois = new ObjectInputStream(new FileInputStream(file));
        HungryInstance hungryInstance2 = (HungryInstance) ois.readObject();
        System.out.println("hungryInstance2 = " + hungryInstance2);


        System.out.println(instance==hungryInstance2);
    }
}